﻿using MyFileView.Core.Constant;
using MyFileView.Core.Core;
using MyFileView.Shard;
using MyFileView.Shard.Model;

using MyFleView.Core;
using MyFleView.Service;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;

namespace MyFleView.Controllers
{
    public class HomeController : Controller
    {

        private IConvertFileFactory _convertFileFactory;
        private IConvertFileService _convertFileService;
        private IDbService _dbService;
        public HomeController(IDbService db, IConvertFileService convertFileService,IConvertFileFactory convertFileFactory)// 
        {
            _convertFileFactory=    convertFileFactory;
           
            _dbService = db;
            _convertFileService = convertFileService;
        }
        public ActionResult Index()
        {
            return View();
        }

        #region 文件操作
        public  ActionResult Preview(PreviewFileRequestModel uploadFileModel)
        {
         
            if (ModelState.IsValid)
            {
                var context = _convertFileFactory.ConvertFileContext;
                var fileInfo = ConvertFileInfo.Create(uploadFileModel,Request);
                context.FileInfo = fileInfo;
                context.AfterConvertPathHandle.ConvertFileInfo= fileInfo;
                //  _convertFileFactory.HttpContext = HttpContext;
                var result =  _convertFileService.ConvertFilePrepare();
               
                //  return View(result+".cshtml");
                return  RedirectToAction(context.AfterConvertPathHandle.Action, RouteConstant.Preview);
               // Response.Redirect();
      

            }
            
             return View();
        }

        /// <summary>
        /// 查询所有文件
        /// </summary>
        /// <returns></returns>
        public JsonResult SelectAllFile()
        {
            var data = _dbService.SelectTable();
            List<UploadFileModel> UploadFileModelList = new List<UploadFileModel>();
            for (int i = 0; i < data.Rows.Count; i++)
            {
                var uploadModel = new UploadFileModel();
                uploadModel.FilePath = data.Rows[i]["filePath"].ToString();
                uploadModel.FileSuffix = data.Rows[i]["suffix"].ToString();
                uploadModel.FileName = data.Rows[i]["fileName"].ToString();
                uploadModel.FileSizeByte =(long)data.Rows[i]["fileSizeByte"];
                uploadModel.ContentType = data.Rows[i]["contentType"].ToString();
                uploadModel.Id = data.Rows[i]["Id"].ToString();
                UploadFileModelList.Add(uploadModel);
            }
            return Json(UploadFileModelList, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadFile()
        {
            var file = Request.Files["file"];
            // HttpPostedFileBase

            var result = SavePhysicalFile(file);
            SaveDbFile(result);

            return Json(new { code = 200 });
        }

        public UploadFileModel SavePhysicalFile(HttpPostedFileBase fileBase)
        {
            string fileName = fileBase.FileName;
            string fileType = fileBase.ContentType;
            string suffix = fileName.Substring(fileName.LastIndexOf('.') + 1);
            string path = Request.PhysicalApplicationPath + PathConstant.File+ PathConstant.Slash + fileName;
            if (!Directory.Exists(path)) 
            {
                Directory.CreateDirectory(path);
            }
            //   var filestream = new FileStream(file.InputStream);
            fileBase.SaveAs($"{path}/{fileName}");
            return new UploadFileModel()
            {
                FilePath = path,
                ContentType = fileType,
                FileSuffix = suffix.ToLower(),
                FileName = fileName,
                FileSizeByte = fileBase.ContentLength
            };
        }
        public void SaveDbFile(UploadFileModel uploadFile)
        {
            int val = _dbService.fillTable(uploadFile);
        }

        #endregion

        public ActionResult About()
        {

            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}